/**
 * There are <a href="https://github.com/thinkgem/jeesite">JeeSite</a> code generation
 */
package com.boxin.ims.modules.recharge.web;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.thinkgem.jeesite.common.config.Global;
import com.thinkgem.jeesite.common.persistence.Page;
import com.thinkgem.jeesite.common.web.BaseController;
import com.thinkgem.jeesite.modules.sys.entity.User;
import com.thinkgem.jeesite.modules.sys.utils.UserUtils;
import com.boxin.ims.modules.recharge.entity.RechargeConfig;
import com.boxin.ims.modules.recharge.service.RechargeConfigService;

/**
 * 会员充值配置Controller
 * @author Jakemanse
 * @version 2014-03-03
 */
@Controller
@RequestMapping(value = Global.ADMIN_PATH+"/recharge/rechargeConfig")
public class RechargeConfigController extends BaseController {

	@Autowired
	private RechargeConfigService rechargeConfigService;
	
	@ModelAttribute
	public RechargeConfig get(@RequestParam(required=false) Long id) {
		if (id != null){
			return rechargeConfigService.get(id);
		}else{
			return new RechargeConfig();
		}
	}
	
	@RequiresPermissions("recharge:rechargeConfig:view")
	@RequestMapping(value = {"list", ""})
	public String list(RechargeConfig rechargeConfig, HttpServletRequest request, HttpServletResponse response, Model model) {
		User user = UserUtils.getUser();
		if (!user.isAdmin()){
			rechargeConfig.setUser(user);
		}
        Page<RechargeConfig> page = rechargeConfigService.find(new Page<RechargeConfig>(request, response), rechargeConfig); 
        model.addAttribute("page", page);
		return "modules/recharge/rechargeConfigList";
	}

	@RequiresPermissions("recharge:rechargeConfig:view")
	@RequestMapping(value = "form")
	public String form(RechargeConfig rechargeConfig, Model model) {
	
		User user = UserUtils.getUser();
		if (rechargeConfig.getUser() == null || rechargeConfig.getUser().getId() <1){
			rechargeConfig.setUser(user);
		}
		
		model.addAttribute("rechargeConfig", rechargeConfig);
		return "modules/recharge/rechargeConfigForm";
	}

	@RequiresPermissions("recharge:rechargeConfig:edit")
	@RequestMapping(value = "save")
	public String save(RechargeConfig rechargeConfig, Model model, RedirectAttributes redirectAttributes) {
		if (!beanValidator(model, rechargeConfig)){
			return form(rechargeConfig, model);
		}
		rechargeConfigService.save(rechargeConfig);
		addMessage(redirectAttributes, "保存会员充值配置'" + rechargeConfig.getName() + "'成功");
		return "redirect:"+Global.ADMIN_PATH+"/modules/recharge/rechargeConfig/?repage";
	}
	
	@RequiresPermissions("recharge:rechargeConfig:edit")
	@RequestMapping(value = "delete")
	public String delete(Long id, RedirectAttributes redirectAttributes) {
		rechargeConfigService.delete(id);
		addMessage(redirectAttributes, "删除会员充值配置成功");
		return "redirect:"+Global.ADMIN_PATH+"/modules/recharge/rechargeConfig/?repage";
	}

}
